package com.lyhengtongwl.zqsnews.ui.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lyhengtongwl.zqsnews.App;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.base.BaseActivity;
import com.lyhengtongwl.zqsnews.entity.NewsBaseEntity;
import com.lyhengtongwl.zqsnews.net.MyCallback;
import com.lyhengtongwl.zqsnews.net.Task;
import com.lyhengtongwl.zqsnews.utils.NewsToastUtils;
import com.lyhengtongwl.zqsnews.utils.RegexUtils;
import com.lyhengtongwl.zqsnews.utils.SPUtils;
import com.lyhengtongwl.zqsnews.utils.ScreenUtil;
import com.lyhengtongwl.zqsnews.utils.TitleBuilder;
import com.lyhengtongwl.zqsnews.widget.CenterDialog;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Response;

public class AuthenticationActivity extends BaseActivity {
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_idCard)
    EditText etIDCard;
    @BindView(R.id.tv_done)
    TextView tvDone;

    @OnClick({R.id.tv_done})
    public void onViewClicked(View view) {
        if (ScreenUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.tv_done: //进行实名认证
                String realName = etName.getText().toString();
                String idCard = etIDCard.getText().toString();
                if (RegexUtils.isRealIDCard(idCard)) {
                    doneAT();
                    //跳转图片上传
//                    Bundle bundle = new Bundle();
//                    bundle.putString("realName", realName);
//                    bundle.putString("idCard", etIDCard.getText().toString());
//                    NewsFaceRecActivity.forward(this, bundle);
                } else {
                    NewsToastUtils.showToast(AuthenticationActivity.this, "身份证号不合法！");
                }
                break;
        }
    }

    private void doneAT() {
       Dialog dialog = new CenterDialog(R.layout.loading_layout, AuthenticationActivity.this).showLoad("正在进行实名认证...");
        Task.getApiService().toAuth(etIDCard.getText().toString(), etName.getText().toString()).enqueue(new MyCallback<NewsBaseEntity>(AuthenticationActivity.this) {
            @Override
            protected void onSuccess(Response<NewsBaseEntity> response) {
                try {
                    dialog.dismiss();
                    if (("1").equals(response.body().getCode())) {
                        NewsToastUtils.showToast(AuthenticationActivity.this, "认证成功！");
                        SPUtils.put(App.getContext(), "authStatus", "1");
                        finish();
                    } else {
                        NewsToastUtils.showToast(AuthenticationActivity.this, response.body().getMsg());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    dialog.dismiss();
                }
            }

            @Override
            protected void onFailure(Throwable t) {
                dialog.dismiss();
            }
        });
    }

    @Override
    public void setListen() {
        etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                setBindStatus();
            }
        });
        etIDCard.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                setBindStatus();
            }
        });
    }

    private void setBindStatus() {
        String metName = etName.getText().toString();
        String metIDCard = etIDCard.getText().toString();
        if (TextUtils.isEmpty(metName) || TextUtils.isEmpty(metIDCard)) {
            tvDone.setEnabled(false);
        } else {
            tvDone.setEnabled(true);
        }
    }

    @Override
    protected boolean isStatusBarBlack() {
        return true;
    }

    @Override
    public void setTitleBarColor() {
        new TitleBuilder(this).setTitleText("实名认证")
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_auth;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }
}
